package io.renren.modules.lanling.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import cn.hutool.core.lang.Validator;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.lanling.entity.SubjectEntity;
import io.renren.modules.lanling.service.SubjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.lanling.entity.QuestionEntity;
import io.renren.modules.lanling.service.QuestionService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import static io.renren.modules.sys.shiro.ShiroUtils.getUserEntity;
import static io.renren.modules.sys.shiro.ShiroUtils.getUserId;


/**
 * 题目记录表（记录顺序练习的题目）
 *
 * @author LANLING
 * @email lanling@gmail.com
 * @date 2018-10-22 10:28:14
 */
@RestController
@RequestMapping("lanling/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    @Autowired
    private SubjectService subjectService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("lanling:question:list")
    public R list(@RequestParam Map<String, Object> params) {

        //参数处理
        Long userId = getUserId();
        params.put("createUserId", userId);
        Object name = params.get("name");
        if (Validator.isEmpty(name)) {
            params.remove("name");
        }
        Object subjectId = params.get("subjectId");
        if (Validator.isEmpty(subjectId)) {
            params.remove("subjectId");
        }

        PageUtils page = questionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("lanling:question:info")
    public R info(@PathVariable("id") Integer id) {
        QuestionEntity question = questionService.queryObject(id);
        return R.ok().put("question", question);
    }

    /**
     * 保存(事务)
     */
    @RequestMapping("/save")
    @RequiresPermissions("lanling:question:save")
    @Transactional(rollbackFor = Exception.class)
    public R save(@RequestBody QuestionEntity question) {

         question.setCreateDate(new Date());
        questionService.insert(question);

        //对应的题库数量加1
        Integer subjectId = question.getSubjectId();
        SubjectEntity subjectEntity = subjectService.selectById(subjectId);
        if (subjectEntity != null) {

            //获取题目类型:1-单选;2-多选;3-判断题
            int radioType = 1;
            int checkType = 2;
            int judegType = 3;

            Integer optionType = question.getOptionType();
            if (optionType == radioType) {
                subjectEntity.setRadioNumber(subjectEntity.getRadioNumber() + 1);
            } else if (optionType == checkType) {
                subjectEntity.setCheckNumber(subjectEntity.getCheckNumber() + 1);
            } else if (optionType == judegType) {
                subjectEntity.setJudegNumber(subjectEntity.getJudegNumber() + 1);
            }

            subjectEntity.setSumQuestionNumber(subjectEntity.getRadioNumber() + subjectEntity.getCheckNumber() + subjectEntity.getJudegNumber());
            subjectService.updateAllColumnById(subjectEntity);
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("lanling:question:update")
    public R update(@RequestBody QuestionEntity question) {
        ValidatorUtils.validateEntity(question);
        question.setLastModifyDate(new Date());
        questionService.update(question);

        return R.ok();
    }

    /**
     * 删除(事务)
     */
    @RequestMapping("/delete")
    @RequiresPermissions("lanling:question:delete")
    @Transactional(rollbackFor = Exception.class)
    public R delete(@RequestBody Integer[] ids) {

        for (Integer id : ids) {
            QuestionEntity question = questionService.queryObject(id);
            //对应的题库数量减去1
            Integer subjectId = question.getSubjectId();
            SubjectEntity subjectEntity = subjectService.selectById(subjectId);
            if (subjectEntity != null) {
                //获取题目类型
                //获取题目类型:1-单选;2-多选;3-判断题
                int radioType = 1;
                int checkType = 2;
                int judegType = 3;

                Integer optionType = question.getOptionType();
                if (optionType == radioType) {
                    subjectEntity.setRadioNumber(subjectEntity.getRadioNumber() - 1);
                } else if (optionType == checkType) {
                    subjectEntity.setCheckNumber(subjectEntity.getCheckNumber() - 1);
                } else if (optionType == judegType) {
                    subjectEntity.setJudegNumber(subjectEntity.getJudegNumber() - 1);
                }
                subjectEntity.setSumQuestionNumber(subjectEntity.getRadioNumber() + subjectEntity.getCheckNumber() + subjectEntity.getJudegNumber());
                subjectService.updateAllColumnById(subjectEntity);
            }
        }

        questionService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
